package com.itheima.studentsystem;

import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.Scanner;

public class StudentSystem {
    public static void startStudentSystem() {
        ArrayList<Student> list = new ArrayList<>();
        while (true) {
            System.out.println("---------------欢迎来到黑马学生管理系统-----------------");
            System.out.println("1：添加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查询学生");
            System.out.println("5：退出");
            System.out.println("请输入您的选择:");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose){
                case "1"-> add(list);
                case "2"-> delete(list);
                case "3"-> update(list);
                case "4"-> query(list);
                case "5"-> {System.out.println("退出");
                System.exit(0);
                }
                default -> System.out.println("没有这个选项，请重新选择");
            }
        }
    }

    private static void update(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改学生的id");
        String id = sc.next();
        int index = getIdIndex(list,id);
        if(index>=0){
            Student s = list.get(index);
            System.out.println("请输入要修改的学生姓名");
            String newName = sc.next();
            s.setName(newName);
            System.out.println("请输入要修改的学生年龄");
            int newAge = sc.nextInt();
            s.setAge(newAge);
            System.out.println("请输入要修改的学生地址");
            String newAddress = sc.next();
            s.setAddress(newAddress);
            System.out.println("修改成功");
        }else{
            System.out.println("需要修改的学生id不存在");
        }
        return;
    }

    private static int getIdIndex(ArrayList<Student> list,String id) {
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if(s.getId().equals(id)){
                return i;
            }
        }
        return -1;
    }

    private static void delete(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学生id");
        String id = sc.next();
        int index = getIdIndex(list, id);
        if(index>=0){
            list.remove(index);
        }else {
            System.out.println("您输入的学生id不存在");
        }
    }

    private static void add(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        String sId;
        while (true) {
            System.out.println("请输入学生的id");
            sId = sc.next();
            boolean flag = contains(list,sId);
            if (flag){
                System.out.println("当前学生id已存在，请重新输入学生id");
            }else {
                break;
            }
        }
        //走到这说明学生id不存在，继续添加姓名，年龄，家庭住址
        System.out.println("请输入学生的姓名");
        String name = sc.next();
        System.out.println("请输入学生的年龄");
        int age = sc.nextInt();
        System.out.println("请输入学生的家庭住址");
        String address = sc.next();
        Student s = new Student(sId,name,age,address);
        list.add(s);
        return;
    }

    private static boolean contains(ArrayList<Student> list, String sId) {
        return getIdIndex(list,sId)>=0;
    }

    public static void query(ArrayList<Student> list){
        if(list.size()==0){
            System.out.println("当前无学生信息，请添加后再查询");
            return;
        }
        //遍历集合获取每个学生对象
        System.out.println("id\t\t姓名\t\t年龄\t\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            //通过学生对象获取学生信息
            System.out.println(s.getId()+"\t\t"+s.getName()+"\t\t"+s.getAge()+"\t\t"+s.getAddress());
        }


    }
}
